
#include "HttpTools.h"
#include "HTInterface.h"

//imuse 20100902 add start
typedef struct HTTP_LINK_tag
{
	int nIndex;
	HTTP_TOOLS *pHttp;
	struct HTTP_LINK_tag *pNext;
}HTTP_LINK;


HTTP_LINK *g_pHttpLink;
int g_nHttpIndex = 1;

TBOOL g_bLock = FALSE;

TBOOL G_GetLock()
{
	return g_bLock;
}

void G_Lock()
{
	while (g_bLock)
	{
		printf(".");
		SLEEP(10);
	}
	g_bLock = TRUE;
}

void G_UnLock()
{
	g_bLock = FALSE;
}

int HttpTools_AddHttpLinkNode(HTTP_TOOLS *pHttp)
{
	G_Lock();
	HTTP_LINK *pLink = (HTTP_LINK*)Tools_Malloc(sizeof(HTTP_LINK));
	pLink->pHttp = pHttp;
	
	int nIndex = g_nHttpIndex++;
	HTTP_LINK *pTemp = g_pHttpLink;
	int nCount = 0;
	while(pTemp)
	{
		nCount++;
		if(!pTemp->pNext)
		{
			//printf("\n conn Count:%d",nCount);
			break;
		}
		pTemp = pTemp->pNext;
	}
	
	pLink->nIndex = nIndex;
	
	//¡¨Ω”¡¥±Ì
	if(pTemp && pTemp->nIndex>0)
	{
		pTemp->pNext = pLink;
	}
	else
	{
		g_pHttpLink = pLink;
	}
	//printf("\nadd :%d",pLink->nIndex);
	G_UnLock();
	return pLink->nIndex;
}

void HttpTools_RemoveHttpLinkNode(HTTP_TOOLS *pHttp)
{
	G_Lock();
	HTTP_LINK *pTemp = g_pHttpLink;
	HTTP_LINK *pPrve = NULL;
	while(pTemp)
	{
		if(pTemp->nIndex==pHttp->nIndex)
		{
			if(pTemp==g_pHttpLink)
			{
				if(pTemp->pNext)
				{
					g_pHttpLink = pTemp->pNext;
				}
				else
				{
					g_pHttpLink = NULL;
				}
			}
			else if(pPrve)
			{
				pPrve->pNext = pTemp->pNext;
			}
			//printf("\nremove :%d", pTemp->nIndex);
			Tools_Free((void**)&pTemp);
			break;
		}
		pPrve = pTemp;
		pTemp = pTemp->pNext;
	}
	G_UnLock();
}

HTTP_TOOLS* HttpTools_GetHttp(int nIndex)
{
	if(nIndex==0)
	{
		return NULL;
	}
	HTTP_LINK *pTemp = g_pHttpLink;
	while(pTemp)
	{
		if(pTemp->nIndex==nIndex)
		{
			//G_UnLock();
			return pTemp->pHttp;
			
		}
		pTemp = pTemp->pNext;
	}
	return NULL;
}
//imuse 20100902 add end


int HttpTools_GetData(int nIndex, char *pUrl, void *pData, int nLen, char *pType,
					  void(*pResedData)(struct HTTP_TOOLS_tag *pHttp, int nType),
					  void *pCBObj)
{
	//取消网络通信
	int nRetIndex = -1;
	HttpTools_Cancel(nIndex);
	
	if(!pUrl)
	{
		return nRetIndex;
	}
	
	HTTP_TOOLS *pHttp;
	pHttp = Tools_Malloc(sizeof(HTTP_TOOLS));
	
	//复制网址
	pHttp->pUrl = Tools_Malloc(strlen(pUrl)+2);
	strcpy(pHttp->pUrl, pUrl);
	
	//复制发送数据
	pHttp->nSendLen = nLen;
	if(pData && pHttp->nSendLen>0)
	{
		pHttp->pSendData = Tools_Malloc(pHttp->nSendLen+2);
		memcpy(pHttp->pSendData, pData, pHttp->nSendLen);
	}
	
	//复制类型
	if(!pType || strlen(pType)==0)
	{
		if (pHttp->pSendData!=NULL && pHttp->nSendLen>0)
		{
			pHttp->pContentType = Tools_Malloc(strlen(POST_MODE_TYPE)+2);
			strcpy(pHttp->pContentType, POST_MODE_TYPE);
		} 
		else
		{
			pHttp->pContentType = Tools_Malloc(strlen(GET_MODE_TYPE)+2);
			strcpy(pHttp->pContentType, GET_MODE_TYPE);
		}
	}
	else
	{
		pHttp->pContentType = Tools_Malloc(strlen(pType)+2);
		strcpy(pHttp->pContentType, pType);
	}
	
  if(pHttp->pRecvData){//每次请求前得清除上一次的缓存
    Tools_Free(&pHttp->pRecvData);
  }
	
	//回调相关
	pHttp->pCBObj = pCBObj;
	pHttp->pResedData = pResedData;
	
	pHttp->isCancel = FALSE;
	
	//imuse 20100902 add start
	nRetIndex = HttpTools_AddHttpLinkNode(pHttp);
	pHttp->nIndex = nRetIndex;
	//imuse 20100902 add end 
	
	
	//创建异步请求
	HTInterface_getData(pHttp);
	
	return nRetIndex;
}

int HttpTools_GetDataEx(int nIndex,  char *pUrl, void *pData, int nLen, char *pType, char *pHead,
                        void(*pResedData)(struct HTTP_TOOLS_tag *pHttp, int nType),
                        void *pCBObj)
{
  //取消网络通信
  int nRetIndex = -1;
  HttpTools_Cancel(nIndex);
  
  if(!pUrl)
  {
    return nRetIndex;
  }
  
  HTTP_TOOLS *pHttp;
  pHttp = Tools_Malloc(sizeof(HTTP_TOOLS));
  
  //复制网址
  pHttp->pUrl = Tools_Malloc(strlen(pUrl)+2);
  strcpy(pHttp->pUrl, pUrl);
  
  //复制发送数据
  pHttp->nSendLen = nLen;
  if(pData && pHttp->nSendLen>0)
  {
    pHttp->pSendData = Tools_Malloc(pHttp->nSendLen+2);
    memcpy(pHttp->pSendData, pData, pHttp->nSendLen);
  }
  
  //复制类型
  if(!pType || strlen(pType)==0)
  {
    if (pHttp->pSendData!=NULL && pHttp->nSendLen>0)
    {
      pHttp->pContentType = Tools_Malloc(strlen(POST_MODE_TYPE)+2);
      strcpy(pHttp->pContentType, POST_MODE_TYPE);
    }
    else
    {
      pHttp->pContentType = Tools_Malloc(strlen(GET_MODE_TYPE)+2);
      strcpy(pHttp->pContentType, GET_MODE_TYPE);
    }
  }
  else
  {
    pHttp->pContentType = Tools_Malloc(strlen(pType)+2);
    strcpy(pHttp->pContentType, pType);
  }
  
  //复制头
  if(pHead && strlen(pHead) > 0) {
    pHttp->pHeadData = Tools_Malloc(strlen(pHead)+1);
    strcpy(pHttp->pHeadData, pHead);
  }
  
  //回调相关
  pHttp->pCBObj = pCBObj;
  pHttp->pResedData = pResedData;
  
  pHttp->isCancel = FALSE;
  
  //imuse 20100902 add start
  nRetIndex = HttpTools_AddHttpLinkNode(pHttp);
  pHttp->nIndex = nRetIndex;
  //imuse 20100902 add end
  //创建异步请求
  HTInterface_getData(pHttp);	
  return nRetIndex;
}

void HttpTools_Cancel(int nIndex)
{
	G_Lock();
	HTTP_TOOLS *pHttp;
	pHttp = HttpTools_GetHttp(nIndex);
	
	if(pHttp)
	{
		pHttp->isCancel = TRUE;
		
		//结束请求
		HTInterface_cancel(pHttp);
	}
	G_UnLock();
}

void HttpTools_ResedData(struct HTTP_TOOLS_tag *pHttp, int nType)
{
	pHttp->pResedData(pHttp, nType);
	
	
	//释放空间
	if(pHttp->pUrl)
	{
		Tools_Free(&pHttp->pUrl);
	}
	if(pHttp->pSendData)
	{
		Tools_Free(&pHttp->pSendData);
	}
	if(pHttp->pContentType)
	{
		Tools_Free(&pHttp->pContentType);
	}
	if(pHttp->pRecvData)
	{
		Tools_Free(&pHttp->pRecvData);
	}
	
	//imuse 20100902 add start
	HttpTools_RemoveHttpLinkNode(pHttp);
	//imuse 20100902 add end
	
	
	//free
	HTInterface_free(pHttp);
	
	Tools_Free(&pHttp);
}

